var Tools = function () {
    /* 获取经纬度 */
    function get_address(callback) { //当前位置的经纬度
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var res = {};
                res.longitude = r.point.lng;
                res.latitude = r.point.lat;
                if (callback) {
                    callback(res);
                }
            } else {
                alert('failed' + this.getStatus());
            }
        }, {
            enableHighAccuracy: true
        })
    };

    /* 根据经纬度获取具体位置 */
    function get_address_detail(lat, lot, cb) {
        var geoc = new BMap.Geocoder();
        var pt = new BMap.Point(lat, lot);
        geoc.getLocation(pt, function (rs) {
            var detail = rs.addressComponents;
            if (cb) {
                cb(detail);
            }
        });
    }
    
    //两点之间的距离
    function get_distance(lon1, lat1, lon2, lat2) {
        var pointA = new BMap.Point(106.486654, 29.490295);
        var pointB = new BMap.Point(lon2, lat2); // 创建点坐标B--江北区
        var dis = (map.getDistance(pointA, pointB)).toFixed(2);
        return dis
    }

    /* 降低频率 */
    function debounce(func, wait, immediate) {
        var timeout;
        return function () {
            var context = this,
                arg = arguments;
            var later = function () {
                !immediate && func.apply(context, arg);
            }
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            callNow && func.apply(context, arg);
        }
    }

    /* -------获取url中参数----- 
        arg, 需要获取的参数名
    */
    function getArg(arg) {
        var url = location.href;
        var args = url.split("?")[1];
        if (args) {
            var argsArray = args.split("&");
        }
        var result = {};
        for (p in argsArray) {
            var a = argsArray[p].split("=");
            var b = a[1];
            if (a.length >= 3) {
                for (var j = 2; j < a.length; j++) {
                    b = b + '=' + a[j];
                }
                result[a[0]] = b;
            } else {
                result[a[0]] = a[1];
            }
        }
        return result[arg];
    }
    /* 计数器 
    
        trg, 计数对象
    */
    function counter(trg) {

        var count = 60;
        var old = trg.attr('data-text'); //对象的old text
        console.log(old);
        trg.addClass('btn-disabled'); //只能点击一次
        
        return function () {
            var timer = setInterval(function () {
                count--;
                if (count == 0) {
                    clearInterval(timer);
                    trg.text(old);
                    trg.removeClass('btn-disabled');
                } else {
                    trg.text(count);
                }
            }, 1000);
        }
    }

    /* ajax请求 
        {
            method, 方法
            sendSuc, 回掉
            url, 地址
            data, 参数
        }
    */
    function doAjax(ajaxObj) {
        if(!ajaxObj.url) {
            return 
        }
        var method = ajaxObj.method || "post";
        var sendSuc = ajaxObj.sendSuc || null;
        var url = ajaxObj.url;
        var data = ajaxObj.data;

        $.ajax({
            url: url,
            type: method,
            data: data,
            success: function (result) {
                sendSuc(result);
            }
        });
    }

    return {
        get_address: get_address,
        get_address_detail: get_address_detail,
        get_distance: get_distance,
        debounce: debounce,
        getArg: getArg,
        doAjax: doAjax,
        counter: counter
    }
}();